package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;


    @RequestMapping("/getUsername")
    public Result getUsername(){
        try{
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true,MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    // 查询用户数据的分页查询
    @PreAuthorize("hasAuthority('USER_QUERY')")
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return userService.pageQuery(queryPageBean);
    }

    // 添加一个新用户
    @PreAuthorize("hasAuthority('USER_ADD')")
    @PostMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user,Integer[] roleIds){
        try{
            userService.add(user,roleIds);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        }catch (Exception e){
            if(e.getMessage().equals("用户已存在")){
                return new Result(false, MessageConstant.ADD_USER_FAIL+",用户已存在");
            }
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
    }

    // 根据用户id 查询关联的角色
    @PreAuthorize("hasAuthority('USER_QUERY')")
    @GetMapping("/findRolesByUserId")
    public Result findRolesByUserId(Integer userId){
        try{
            List<Integer> roleIds = userService.findRolesByUserId(userId);
            return new Result(true, MessageConstant.GET_ROLE_SUCCESS, roleIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLE_FAIL);
        }
    }

    // 更新用户所对应的角色
    @PreAuthorize("hasAuthority('USER_EDIT')")
    @RequestMapping("/updateRoleAndUserId")
    public Result updateRoleAndUserId(Integer[] roleIds, @RequestBody com.itheima.pojo.User user){
        try{
            Integer userId = user.getId();
            userService.updateRoleAndUserId(userId, roleIds,user);
            return new Result(true, MessageConstant.SET_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.SET_ROLE_FAIL);
        }
    }
    //删除用户
    @PreAuthorize("hasAuthority('USER_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
            return new Result(true,"删除用户成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除用户失败");
        }
    }

    //根据id查询用户
    @PreAuthorize("hasAuthority('USER_QUERY')")
    @RequestMapping("/findUserById")
    public Result findUserById(Integer id){
        try{
            com.itheima.pojo.User user = userService.findUserById(id);
            user.setPassword("");
            return new Result(true,MessageConstant.GET_USER_SUCCESS,user);
        }catch (Exception e){
            return new Result(false,MessageConstant.GET_USER_FAIL);
        }
    }

    //修改密码
    @RequestMapping("/updatePassword")
    public Result updatePassword(String username,@RequestBody Map map){
        try{
            userService.updatePassword(map,username);
        }catch (Exception e){
            if(e.getMessage().equals("旧密码输入错误")){
                return new Result(false,"旧密码输入错误");
            }
            if(e.getMessage().equals("两次密码输入不一致")){
                return new Result(false,"两次密码输入不一致");
            }
            if(e.getMessage().equals("新密码不能与旧密码相同")){
                return new Result(false,"新密码不能与旧密码相同");
            }
        }
        return new Result(true,"密码修改成功");
    }



}
